﻿using System;
using System.IO;
using System.Text;
using System.Linq;
namespace RpcHelper.Error
{
    internal static class ErrorHelper
    {
        internal static void SaveFile(this ILocalError error)
        {
            ErrorMsg[] msgs = error.Errors;
            string[] groups = msgs.Select(a =>a.ErrorCode.Substring(0,a.ErrorCode.IndexOf('.'))).Distinct().ToArray();
            StringBuilder xml = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xml.AppendLine("<errorlist lang=\""+error.Lang+"\">");
            groups.ForEach(a =>
            {
                xml.AppendFormat("<{0}>\r\n", a);
                string str = string.Format("{0}.", a);
                ErrorMsg[] t = msgs.FindAll(b => b.ErrorCode.StartsWith(str));
                t.ForEach(b =>
                {
                    xml.AppendFormat("<error><id>{0}</id><code>{1}</code><msg>{2}</msg></error>\r\n", b.ErrorId, b.ErrorCode.Remove(0, str.Length), b.Text);
                });
                xml.AppendFormat("</{0}>\r\n", a);
            });
            xml.AppendLine("</errorlist>");
            string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, string.Format("ErrorMsg_{0}.xml", error.Lang));
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            using (StreamWriter writer = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)))
            {
                writer.Write(xml);
                writer.Flush();
                writer.Close();
            }

        }
        internal static string FormatError(string error, out string param)
        {
            int i = error.Length - 1;
            if (error[i] != ']')
            {
                param = null;
                return error;
            }
            int b = error.LastIndexOf('[') + 1;
            param = error.Substring(b, i - b);
            return error.Substring(0, b - 1);
        }
        internal static string TryGetCode(this Func<IErrorEvent> action, long errorId)
        {
            if (action == null)
            {
                return null;
            }
            return action().FindErrorCode(errorId);
        }
        internal static long TryGetId(this Func<IErrorEvent> action, string code)
        {
            if (action == null)
            {
                return 0;
            }
            return action().FindErrorId(code);
        }
        internal static void TriggerDrop(this Func<IErrorEvent> action,ErrorMsg msg)
        {
            if (action == null)
            {
                return;
            }
            IErrorEvent ev = action();
            ev.DropError(msg);
        }
        internal static ErrorMsg GetError(this Func<IErrorEvent> action, string code,string lang)
        {
            if (action == null)
            {
                return null;
            }
            IErrorEvent ev = action();
            return ev.GetError(code, lang);
        }
    }
}
